package models;

import global.ConfigData;

import java.io.IOException;
import java.io.Serializable;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import lib.Field;
import lib.persistence.ExcelConnector;
import lib.persistence.NotFoundException;
import lib.persistence.commands.GetRow;
import lib.reflection.Reflector;

public class Person extends ModelAbstract implements Serializable
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	String tableName = ConfigData.PERSON_TABLE;
	public enum Sex {Male, Female, Other};
	public final static String WORKSHEET_NAME = "Person";
	
	private String firstName;
	private String lastName;
	private String employeeNumber;
	private String phone;
	private String title;
	private Sex sex;
	private String description;
	private Date createdAt;


	public Person()
	{
		setId(-1);
		firstName = "";
		lastName = "";
		employeeNumber = "";
		phone = "";
		title = "";
		sex = Sex.Other;
		description = "";
	}
	public Person(String first_Name, String last_Name,String employee_Number, String phone_Number, String job_Title, Sex sex, String descr) {
		super();
		firstName = first_Name;
		lastName = last_Name;
		employeeNumber = employee_Number;
		phone = phone_Number;
		title = job_Title;
		this.sex  = sex;
		description = descr;
		
	}
	public Person(Person p) {
		this.firstName = p.firstName;
		this.lastName = p.lastName;
		this.employeeNumber = p.employeeNumber;
		this.phone = p.phone;
		this.title = p.title;
		this.sex  = p.sex;
		this.description = p.description;
		this.createdAt = p.createdAt;
	}
	
	public String getFirstName() 
	{
		return firstName;
	}

	public void setFirstName(String first_Name) 
	{
		firstName = first_Name;
	}

	public String getLastName() 
	{
		return lastName;
	}

	public void setLastName(String last_Name) 
	{
		lastName = last_Name;
	}

	public String getEmployeeNumber() 
	{
		return employeeNumber;
	}

	public void setEmployeeNumber(String employee_Number) 
	{
		employeeNumber = employee_Number;
	}

	public String getPhone() 
	{
		return phone;
	}

	public void setPhone(String phone_Number) 
	{
		phone = phone_Number;
	}

	public String getTitle() 
	{
		return title;
	}

	public void setTitle(String job_Title) 
	{
		title = job_Title;
	}
	
	public void setSex(Sex sex) {
		this.sex = sex;
	}
	public void setSex(String sex)
	{
		if (sex.equalsIgnoreCase("male"))
			this.sex = Sex.Male;
		else if (sex.equalsIgnoreCase("female"))
			this.sex = Sex.Female;
		else if (sex.equalsIgnoreCase("other"))
			this.sex = Sex.Other;
		
	}

	public Sex getSex() {
		return sex;
	}
	
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
	
	// End Getters and setters
	
	public boolean equals (Person o)
	{
		if (o == null)
			return false;
		if ( !firstName.equalsIgnoreCase(o.getFirstName()))
				return false;
		if (!lastName.equalsIgnoreCase(o.getLastName()))
				return false;
		if (!employeeNumber.equalsIgnoreCase(o.getEmployeeNumber()))
			return false;
		if (!phone.equalsIgnoreCase(o.getPhone()))
			return false;
		if (!title.equalsIgnoreCase(o.getTitle()))
			return false;
		if (sex != o.getSex())
			return false;
		if (!description.equalsIgnoreCase(o.getDescription()))
			return false;
		
			return true;
	}
	public static Person fromExcel(ExcelConnector ec, int rowNumber) throws Exception {
		//@TODO: move to config
		ec.setWorksheet(WORKSHEET_NAME);
		List<Field> fields = ec.getRow(rowNumber);
		Person p = new Person();
		return (Person) Reflector.setReflectedMethods(fields, p, Class.forName("models.Person"));		
	}
	
	public void setCreatedAt(Date createdAt) {
		this.createdAt = createdAt;
	}
	public void setCreatedAt(String createdAt) throws ParseException {
		SimpleDateFormat d = new SimpleDateFormat("MM/dd/yy");
		
		this.createdAt = d.parse(createdAt);
	}
	public Date getCreatedAt() {
		return createdAt;
	}
	public  void setId(int id) {
		this.id = id;
	}
	public  void setId(String id) {
		this.id = Integer.parseInt(id);
	}
	public  int getId() {
		return this.id;
	}
	protected String getTableName()
	{
		return ConfigData.PERSON_TABLE;
	}
	protected ArrayList<Field> getSqlReadyFields()
	{
//		
//		private String firstName;
//		private String lastName;
//		private String employeeNumber;
//		private String phone;
//		private String title;
//		private Sex sex;
//		private String description;
//		private Date createdAt;
		
		ArrayList<Field> al = new ArrayList<Field>();
		Field f;
		f = new Field();
		al.add(f.setName("obj").setType("blob").setObjValue(this));

		f = new Field();
		al.add(f.setName("name").setType("string").setValue(this.firstName + ' ' + this.lastName));
		
		//last name
//		f = new Field();
//		al.add(f.setName("lastName").setType("string").setValue(this.lastName));
		
		// creator. do not implement
//		f = new Field();
//		al.add(f.setName("creator").setType("integer").setValue(this.creator.getID()));
		
//		// creation date
//		f = new Field();
//		al.add(f.setName("creationDate").setType("string").setValue(this.createdAt.toString()));
		
		// title
		f = new Field();
		al.add(f.setName("title").setType("string").setValue(this.title));
		
		// dateofBirth: note, DOB not implemented
//		f = new Field();
//		al.add(f.setName("dateofbirth").setType("string").setValue());
		
		// gender
//		f = new Field();
//		al.add(f.setName("gender").setType("string").setValue(this.sex.toString()));
		
		return al;
	}
	public static Person find(int id) throws NotFoundException, IOException, ClassNotFoundException, SQLException   
	{
		GetRow gr = new GetRow();
		return (Person) gr.findObjectById(id, ConfigData.PERSON_TABLE, "obj");
	}
	public static Person findLatest() throws SQLException, IOException, ClassNotFoundException {
		GetRow gr = new GetRow();
		ArrayList<Object>  set = gr.findObjectsByLatest(1,"id DESC", ConfigData.PERSON_TABLE ,"obj");
		return (Person) set.get(0);
	}
	

}
